package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Reporter;

import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: Lazarenok
 * Date: 21.11.13
 * Time: 20:55
 * To change this template use File | Settings | File Templates.
 */
public class HomePage {

    public WebDriver driver;
    public WebElement accounts;
    public WebElement logOut;

    public HomePage(WebDriver driver) {
        this.driver = driver;
    }

    public void goHome() {
        driver.get("https://www.buxfer.com/");
    }

    public HomePage signup(String email, String password) throws InterruptedException {
        driver.get("https://www.buxfer.com/");
        WebElement signupBtn = driver.findElement(By.className("signup"));
        signupBtn.click();
        WebElement signupEmailField = driver.findElement(By.id("signupEmail"));
        signupEmailField.sendKeys(email);
        WebElement passwordField = driver.findElement(By.id("signup_password"));
        passwordField.sendKeys(password);
        WebElement createAccountBtn = driver.findElement(By.linkText("Create Account"));
        createAccountBtn.click();

        TimeUnit.SECONDS.sleep(3);

        driver.get("https://www.buxfer.com/");
        //WebElement popUpClose = driver.findElement(By.linkText("Cancel"));
        // WebElement popUpClose = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/table/tbody/tr[3]/td/div/div"));
        //popUpClose.click();
        return new HomePage(driver);
    }

    public HomePage login(String username, String password) throws InterruptedException {

        driver.get("https://www.buxfer.com/");
        WebElement loginField = driver.findElement(By.id("login_username"));
        loginField.sendKeys(username);
        WebElement passwordField = driver.findElement(By.id("login_password"));
        passwordField.sendKeys(password);
        WebElement loginButton = driver.findElement(By.linkText("Login"));
        loginButton.click();
        TimeUnit.SECONDS.sleep(3);
        driver.get("https://www.buxfer.com/");
        // WebElement popUpClose = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/table/tbody/tr[3]/td/div/div"));
        //popUpClose.click();
        return new HomePage(driver);
    }

    public boolean checkLoginStatus() {

        String targetURL = driver.getCurrentUrl();
        if (targetURL.contains("www.buxfer.com/slogin"))
        {
            return false;
        }
        else {
            return true;
        }
    }


    public HomePage logout() {
        accounts = driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div[2]/div[3]/div/div/div/div/div/div"));
        accounts.click();
        logOut = driver.findElement(By.xpath("//div[contains(text(),'Logout')]"));
        logOut.click();
        Reporter.log("loged out");
        return new HomePage(driver);
    }


    public HomePage deleteAccount(String username, String password) throws InterruptedException {

        accounts = driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div[2]/div[3]/div/div/div/div/div/div"));
        accounts.click();
        driver.findElement(By.xpath("//div[contains(text(),'Profile')]")).click();
        driver.findElement(By.linkText("Delete account")).click();
        driver.findElement(By.linkText("Yes, delete my account")).click();
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/table/tbody/tr[3]/td/div[2]/form/div[2]/input")).sendKeys(password);
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/table/tbody/tr[3]/td/div[2]/form/div[4]/textarea")).sendKeys("Automated testing reason");
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/table/tbody/tr[3]/td/div[2]/form/div[5]/div/table/tbody/tr[2]/td/div/a")).click();
        TimeUnit.SECONDS.sleep(1);
        return new HomePage(driver);
    }
}
